using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class BusinessDAL
    {
        public static int Save(BusinessInfo info)
        {
            string sql = string.Format("insert into [business]([NickName],[HeadImg],[ServiceTypeInfo],[VerifyTypeInfo],[UserName],[PrincipalName],[Alias],[BusinessInfo],[QrcodeUrl],[AuthorizationInfo],[AuthorizationAppID],[FuncInfo],[AuthorizerAccessToken],[AuthorizerRefreshToken],[CreateDate]) values(@NickName,@HeadImg,@ServiceTypeInfo,@VerifyTypeInfo,@UserName,@PrincipalName,@Alias,@BusinessInfo,@QrcodeUrl,@AuthorizationInfo,@AuthorizationAppID,@FuncInfo,@AuthorizerAccessToken,@AuthorizerRefreshToken,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@NickName", info.NickName),
                new SqlParameter("@HeadImg", info.HeadImg),
                new SqlParameter("@ServiceTypeInfo", info.ServiceTypeInfo),
                new SqlParameter("@VerifyTypeInfo", info.VerifyTypeInfo),
                new SqlParameter("@UserName", info.UserName),
                new SqlParameter("@PrincipalName", info.PrincipalName),
                new SqlParameter("@Alias", info.Alias),
                new SqlParameter("@BusinessInfo", info.BusinessInfos),
                new SqlParameter("@QrcodeUrl", info.QrcodeUrl),
                new SqlParameter("@AuthorizationInfo", info.AuthorizationInfo),
                new SqlParameter("@AuthorizationAppID", info.AuthorizationAppID),
                new SqlParameter("@FuncInfo", info.FuncInfo),
                new SqlParameter("@AuthorizerAccessToken", info.AuthorizerAccessToken),
                new SqlParameter("@AuthorizerRefreshToken", info.AuthorizerRefreshToken),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar2(sql, paras));
        }
        public static int Update(BusinessInfo info)
        {
            string sql = string.Format("update [business] set [NickName]=@NickName,[HeadImg]=@HeadImg,[ServiceTypeInfo]=@ServiceTypeInfo,[VerifyTypeInfo]=@VerifyTypeInfo,[UserName]=@UserName,[PrincipalName]=@PrincipalName,[Alias]=@Alias,[BusinessInfo]=@BusinessInfo,[QrcodeUrl]=@QrcodeUrl,[AuthorizationInfo]=@AuthorizationInfo,[AuthorizationAppID]=@AuthorizationAppID,[FuncInfo]=@FuncInfo,[AuthorizerAccessToken]=@AuthorizerAccessToken,[AuthorizerRefreshToken]=@AuthorizerRefreshToken where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@NickName", info.NickName),
                new SqlParameter("@HeadImg", info.HeadImg),
                new SqlParameter("@ServiceTypeInfo", info.ServiceTypeInfo),
                new SqlParameter("@VerifyTypeInfo", info.VerifyTypeInfo),
                new SqlParameter("@UserName", info.UserName),
                new SqlParameter("@PrincipalName", info.PrincipalName),
                new SqlParameter("@Alias", info.Alias),
                new SqlParameter("@BusinessInfo", info.BusinessInfos),
                new SqlParameter("@QrcodeUrl", info.QrcodeUrl),
                new SqlParameter("@AuthorizationInfo", info.AuthorizationInfo),
                new SqlParameter("@AuthorizationAppID", info.AuthorizationAppID),
                new SqlParameter("@FuncInfo", info.FuncInfo),
                new SqlParameter("@AuthorizerAccessToken", info.AuthorizerAccessToken),
                new SqlParameter("@AuthorizerRefreshToken", info.AuthorizerRefreshToken),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static BusinessInfo FindOneByID(int id)
        {
            DataTable dt = CommonDAL.FindOneByIDs(id, "[business]");
            return Model(dt);
        }
        public static List<BusinessInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[business]");
            return ListModel(dt);
        }
        public static BusinessInfo FindOneByUserName(string username)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [business] where UserName='{0}'", username));
            return Model(dt);
        }
        public static List<BusinessInfo> FindPage(string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [business].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [business].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[business]", out count);
            return ListModel(dt);
        }
        public static List<BusinessInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [business]"));
            return ListModel(dt);
        }
        public static bool IsExist(string authorizationappid)
        {
            int count = DataConvert.ToInt(CommonDAL.ExecuteScalar(string.Format("select count(1) from [business] where AuthorizationAppID='{0}'", authorizationappid)));
            return count > 0 ? true : false;
        }
        private static BusinessInfo Model(DataRow dr, DataColumnCollection col)
        {
            BusinessInfo info = new BusinessInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.NickName = DataConvert.ToString(dr["NickName"]);
                info.HeadImg = DataConvert.ToString(dr["HeadImg"]);
                info.ServiceTypeInfo = DataConvert.ToString(dr["ServiceTypeInfo"]);
                info.VerifyTypeInfo = DataConvert.ToString(dr["VerifyTypeInfo"]);
                info.UserName = DataConvert.ToString(dr["UserName"]);
                info.PrincipalName = DataConvert.ToString(dr["PrincipalName"]);
                info.Alias = DataConvert.ToString(dr["Alias"]);
                info.BusinessInfos = DataConvert.ToString(dr["BusinessInfo"]);
                info.QrcodeUrl = DataConvert.ToString(dr["QrcodeUrl"]);
                info.AuthorizationInfo = DataConvert.ToString(dr["AuthorizationInfo"]);
                info.AuthorizationAppID = DataConvert.ToString(dr["AuthorizationAppID"]);
                info.FuncInfo = DataConvert.ToString(dr["FuncInfo"]);
                info.AuthorizerAccessToken = DataConvert.ToString(dr["AuthorizerAccessToken"]);
                info.AuthorizerRefreshToken = DataConvert.ToString(dr["AuthorizerRefreshToken"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static BusinessInfo Model(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                return Model(dt.Rows[0], dt.Columns);
            }
            else
            {
                return new BusinessInfo();
            }
        }
        private static List<BusinessInfo> ListModel(DataTable dt)
        {
            List<BusinessInfo> list = new List<BusinessInfo>();
            DataColumnCollection col = dt.Columns;
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Model(row, col));
            }
            return list;
        }
    }
}

